package com.hxzy.shop.commodity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxzy.shop.commodity.convert.MerchantPlugspaylogConvert;
import com.hxzy.shop.commodity.dto.req.MerchantPlugspaylogListReq;
import com.hxzy.shop.commodity.dto.req.PageRequst;
import com.hxzy.shop.commodity.dto.resp.MerchantPlugspaylogResp;
import com.hxzy.shop.commodity.mapper.MerchantPlugsMapper;
import com.hxzy.shop.commodity.mapper.MerchantPlugspaylogMapper;
import com.hxzy.shop.commodity.service.MerchantPlugspaylogService;
import com.hxzy.shop.constant.AuthConstant;
import com.hxzy.shop.entity.MerchantPlugspaylog;
import com.hxzy.shop.utils.WebUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 腾讯云saas订单 服务实现类
 * </p>
 *
 * @author qy
 * @since 2021-08-10
 */
@Service
public class MerchantPlugspaylogServiceImpl extends ServiceImpl<MerchantPlugspaylogMapper, MerchantPlugspaylog> implements MerchantPlugspaylogService {

    @Resource
    private MerchantPlugspaylogMapper mpm;

    @Resource
    private MerchantPlugsMapper merchantPlugsMapper;

    @Override
    public PageInfo<MerchantPlugspaylog> getRechargePage(PageRequst pageRequst) {
        //Mybatis配置过滤了merchant_plugpayslog表，所以需要自己输入商户id
//        HttpServletRequest request = WebUtil.getRequest();
//        String header = request.getHeader(AuthConstant.MERCHANT_ID);
//        Long merchantId = Long.valueOf(header);
        return PageHelper.startPage(pageRequst.getPageNum(), pageRequst.getPageSize())
                .doSelectPageInfo(() -> mpm.selectPageList( pageRequst.getPlugId()));
    }

    @Override
    public PageInfo<MerchantPlugspaylogResp> merchantPlugspayInvoiceList(MerchantPlugspaylogListReq req) {
        PageRequst pageRequst = new PageRequst();
        pageRequst.setPageNum(req.getPageNumber());
        pageRequst.setPageSize(req.getPageSize());
        HttpServletRequest request = WebUtil.getRequest();
        String header = request.getHeader(AuthConstant.MERCHANT_ID);
        Long merchantId = Long.valueOf(header);

        PageInfo<MerchantPlugspaylog> rechargePage = PageHelper.startPage(pageRequst.getPageNum(), pageRequst.getPageSize())
                .doSelectPageInfo(() -> mpm.selectList(new QueryWrapper<MerchantPlugspaylog>().isNotNull("pay_num")
                        .eq("is_invoice", 0).eq("plugin_source",1).or().isNotNull("pay_num").eq("plugin_source",1).isNull("is_invoice").orderByDesc("create_time")));

        List<MerchantPlugspaylog> merchantPlugspaylogs = rechargePage.getList();

        List<MerchantPlugspaylogResp> merchantPlugspaylogRespList = merchantPlugspaylogs.stream().map(merchantPlugspaylog -> {
            MerchantPlugspaylogResp merchantPlugspaylogResp = MerchantPlugspaylogConvert.INSTANCE.toMerchantPlugspaylogResp(merchantPlugspaylog);

            merchantPlugspaylogResp.setDetailsOfCharges(merchantPlugspaylog.getPlugId() == 1 ? "购买处方单" : "购买短信");
            return merchantPlugspaylogResp;
        }).collect(Collectors.toList());



        PageInfo<MerchantPlugspaylogResp> plugspaylogRespPageInfo = new PageInfo<>();
        plugspaylogRespPageInfo.setList(merchantPlugspaylogRespList);
        plugspaylogRespPageInfo.setTotal(rechargePage.getTotal());
        plugspaylogRespPageInfo.setPageNum(rechargePage.getPageNum());
        plugspaylogRespPageInfo.setPageSize(rechargePage.getPageSize());
        plugspaylogRespPageInfo.setEndRow(rechargePage.getEndRow());
        plugspaylogRespPageInfo.setNavigateFirstPage(rechargePage.getNavigateFirstPage());
        plugspaylogRespPageInfo.setNavigateLastPage(rechargePage.getNavigateLastPage());
        plugspaylogRespPageInfo.setNextPage(rechargePage.getNextPage());
        plugspaylogRespPageInfo.setPages(rechargePage.getPages());
        plugspaylogRespPageInfo.setSize(rechargePage.getSize());
        plugspaylogRespPageInfo.setStartRow(rechargePage.getStartRow());
        return plugspaylogRespPageInfo;
    }


}
